package ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import linkfinders.Link;
import linkfinders.YTLink;
import observer.Notification;
import ui.dialog.LinkFoundDialog;
import ui.dialog.YTVideoFoundDialog;
import download.Download;

public class MainWindow extends JFrame implements ListSelectionListener,
		Observer
{

	/**
	 * 
	 */
	private static MainWindow instance=null;
	private static final long serialVersionUID = -2133534539333334278L;
	private JTable table;
	private JTable finished;
	private TableModel model;
	private FinishedModel finishedModel;

	private MainWindow()
	{
		instance = this;
		setTitle("V12 Downloader");
		setLocationRelativeTo(null);
		setSize(600, 500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		setContentPane(panel);
		panel.setLayout(new BorderLayout());

		JPanel bottom = new BottomMenu();
		bottom.setLayout(new FlowLayout(FlowLayout.LEFT,5,10));

		String[] cols =
		{ "Url", "Progression", "Vitesse", "" ,"Taille"};
		model = new TableModel(cols);
		table = new JTable(model);
		table.addMouseListener(new TableMouseListener(table));

		CellRenderer c = new CellRenderer();
		table.setDefaultRenderer(Float.class, c);
		table.setDefaultRenderer(Double.class, c);
		table.setDefaultRenderer(JButton.class, c);
		table.setDefaultRenderer(String.class, c);
		table.setDefaultRenderer(Integer.class, c);
		table.setColumnSelectionAllowed(false);
		table.setCellSelectionEnabled(false);
		table.setRowSelectionAllowed(false);
		table.setShowVerticalLines(false);
		table.setShowHorizontalLines(false);
		table.setRowMargin(0);
		
		
		String cols2[] =
		{ "Url", "Nom du fichier","Type" };
		finishedModel = new FinishedModel(cols2);
		finished = new JTable(finishedModel);
		finished.setDefaultRenderer(String.class, c);
		finished.setColumnSelectionAllowed(false);
		finished.setCellSelectionEnabled(false);
		finished.setRowSelectionAllowed(false);
		finished.setShowVerticalLines(false);
		finished.setShowHorizontalLines(false);
		finished.setRowMargin(0);
		
		Timer t = new Timer(table);
		t.start();

		JScrollPane scroll = new JScrollPane(table);
		JScrollPane scroll2 = new JScrollPane(finished);
		
		JTabbedPane tabs = new JTabbedPane();
		panel.add(tabs, BorderLayout.CENTER);
		
		
		panel.add(bottom,BorderLayout.SOUTH);
		
		JPanel top = new TopMenu();
		
		panel.add(top,BorderLayout.NORTH);
		
		tabs.addTab("En cours", scroll);
		tabs.add("Finis",scroll2);
	}

	public void addDownload(Download dl)
	{
		dl.addObserver(this);
		model.addRow(dl);
		model.fireTableRowsInserted(model.getRowCount()-1, model.getRowCount()-1);
	}


	

	@Override
	public void valueChanged(ListSelectionEvent arg0)
	{

	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable o, Object arg)
	{
		if (arg.getClass() == Notification.class)
		{
			Notification n = (Notification) arg;
			if (o instanceof Download)
			{
				Download dl = (Download)o;
				switch (n.getType())
				{
				case Download.LINKS_NOTIFICATION:
					Class<? extends Object> c =((ArrayList<?>)n.getArg()).get(0).getClass();
					LinkFoundDialog d = null;
					if(c == YTLink.class)
						d = (LinkFoundDialog)new YTVideoFoundDialog(this,(ArrayList<Link>) n.getArg());
					else
						d = new LinkFoundDialog(this,(ArrayList<Link>) n.getArg());

					d.setVisible(true);
					break;
				case Download.FINISHED_NOTIFICATION:
					model.remove(dl);
					model.fireTableRowsDeleted(model.getRowCount()-1, model.getRowCount()-1);
					finishedModel.addDownload(dl);
					finishedModel.fireTableRowsInserted(finishedModel.getRowCount()-1, finishedModel.getRowCount()-1);
					break;
				}
			}
		}

	}

	public static MainWindow getInstance()
	{
		if(instance==null)
			instance=new MainWindow();
		return instance;
	}

	public TableModel getModel()
	{
		return model;
	}
}
